package com.myopen.controller.admin;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseVo;
import com.myopen.config.ConstConfig;
import com.myopen.kit.IpKit;
import com.myopen.kit.ValidateKit;
import com.myopen.model.Log;
import com.myopen.model.User;

public class AdminController extends BaseController{

	@ActionKey("/admin")
	public void admin_index(){
		renderTemplate("/admin/index.html");
	}
	
	@ActionKey("/admin/login")
	public void admin_login(){
		getSession().invalidate();//跳转登录之前清空一下Session信息
		renderCaptcha();
		renderTemplate("/admin/login.html");
	}
	
	@ActionKey("/admin/login/action")
	public void admin_login_action(){
		
		BaseVo baseVo=new BaseVo();
		
		String loginName=getPara("loginName");
		String password=getPara("password");
		if(!validateCaptcha("captchaCode")) {
			baseVo.setCode("3");//验证码错误
			renderJson(baseVo);
			return;
		}
		
		Record user=null;
		if(ValidateKit.isEmail(loginName)){
			user=User.dao.getRecordByEmail(loginName);
		}else if(ValidateKit.isPhone(loginName)) {
			user=User.dao.getRecordByCellphone(loginName);
		}else {
			user=User.dao.getRecordByUsername(loginName);
		}
		
		if(user!=null){
			Log.dao.addShowLog(user.getStr("id"), user.getStr("username")+"登录了系统","admin_login");
			password=User.encryptPassword(password, user.getStr("salt"));
			if(password.equals(user.getStr("password"))){
				//此处设置用户session，当跳转到首页的时候会自动获取角色权限等信息
				setSessionAttr(ConstConfig.SESSION_ADMIN_USER, user);
			}else{
				baseVo.setCode("1");//密码错误
			}
		}else{
			baseVo.setCode("2");//登录名不存在
		}
		
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/loginout")
	public void admin_loginout(){
		getSession().invalidate();
		redirect("/admin/login");
		
	}
	
	@ActionKey("/admin/my/detail/update")
	public void admin_my_detail_update(){
		
		Record user=getAdminUser();
		
		setAttr("vo", user);
		
		renderTemplate("/admin/my_detail.html");
	}
	
	@ActionKey("/admin/my/detail/update/action")
	public void admin_my_detail_update_action(){
		BaseVo baseVo=new BaseVo();
		
		Record user=getAdminUser();
		User u=User.dao.findById(user.getStr("id"));
		
		String nickname=getPara("nickname");
		String email=getPara("email");
		String cellphone=getPara("cellphone");
		
		u.set("nickname", nickname);
		u.set("email", email);
		u.set("cellphone", cellphone);
		
		u.update();
		baseVo.setCode("0");
		
		//同时更新session用户
		user.set("nickname", nickname);
		user.set("email", email);
		user.set("cellphone", cellphone);
		getSession().setAttribute(ConstConfig.SESSION_ADMIN_USER, user);
		
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/my/password/update")
	public void admin_my_password_update(){
		renderTemplate("/admin/my_password.html");
	}
	
	@ActionKey("/admin/my/password/update/action")
	public void admin_my_password_update_action(){
		
		BaseVo baseVo=new BaseVo();
		Record user=getAdminUser();
		User u=User.dao.findById(user.getStr("id"));
		
		String password=getPara("password");
		String password_new=getPara("password_new");
		
		
		String passwordEncrypt=User.encryptPassword(password, u.getStr("salt"));
		if(passwordEncrypt.equals(user.getStr("password"))){
			String password_new_encrypt=User.encryptPassword(password_new, u.getStr("salt"));
			u.set("password", password_new_encrypt);
			u.update();
			baseVo.setCode("0");
		}else {
			baseVo.setCode("1");
		}
		
		renderJson(baseVo);
		
	}
	
}
